package com.itheima.service.db.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.UmsMenu;
import com.itheima.domain.UmsMenu;
import com.itheima.mapper.UmsMenuMapper;
import com.itheima.service.db.UmsMenuService;
import com.itheima.vo.UmsMenuVo;
import com.itheima.vo.UmsMenuVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@DubboService
public class UmsMenuServiceImpl implements UmsMenuService {

    @Autowired
    UmsMenuMapper umsMenuMapper;

    @Override
    public UmsMenu findById(Long id) {
        return umsMenuMapper.selectById(id);
    }

    @Override
    public Page<UmsMenu> findByParentId(Integer parentId, Integer pageNum, Integer pageSize) {
        Page<UmsMenu> page=new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<UmsMenu> lqw=new LambdaQueryWrapper<>();
        lqw.eq(UmsMenu::getParentId,parentId);
        return umsMenuMapper.selectPage(page,lqw);
    }

    @Override
    public List<UmsMenuVo> selectMenuList() {


        return selectByDG(0L);
    }

    @Override
    public void save(UmsMenu umsMenu) {
        umsMenu.setCreateTime(LocalDateTime.now());
        if (umsMenu.getParentId()!=0){
            UmsMenu parent = umsMenuMapper.selectById(umsMenu.getParentId());
            umsMenu.setLevel(parent.getLevel()+1);
        }else {
            umsMenu.setLevel(0);
        }
        umsMenuMapper.insert(umsMenu);
    }

    @Override
    public Boolean delete(Integer id) {
        LambdaQueryWrapper<UmsMenu> lqw=new LambdaQueryWrapper<>();
        lqw.eq(UmsMenu::getParentId,id);
        List<UmsMenu> umsMenus = umsMenuMapper.selectList(lqw);
        if (umsMenus!=null){
            return false;
        }
        umsMenuMapper.deleteById(id);
        return true;
    }

    @Override
    public void updateMenuStatu(Integer id, Integer hidden) {
        UmsMenu umsMenu = new UmsMenu();
        umsMenu.setHidden(hidden);
        umsMenu.setId(id.longValue());
        umsMenuMapper.updateById(umsMenu);
    }

    @Override
    public void updateMenu(Integer id, UmsMenu umsMenu) {
        if (umsMenu.getParentId()!=0){
            UmsMenu parent = umsMenuMapper.selectById(umsMenu.getParentId());
            umsMenu.setLevel(parent.getLevel()+1);
        }else {
            umsMenu.setLevel(0);
        }
        umsMenuMapper.updateById(umsMenu);
    }

    public List<UmsMenuVo> selectByDG(Long id){
        LambdaQueryWrapper<UmsMenu> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UmsMenu::getParentId,id);
        List<UmsMenu> pmsProductCategories = umsMenuMapper.selectList(lqw);
        if (pmsProductCategories.isEmpty()){
            return null;
        }

        List<UmsMenuVo> voList =new ArrayList<>();
        for (UmsMenu UmsMenu : pmsProductCategories) {
            List<UmsMenuVo> childrenVo = selectByDG(UmsMenu.getId());
            UmsMenuVo pVo = new UmsMenuVo();
            BeanUtils.copyProperties(UmsMenu, pVo);
            if (childrenVo!=null){
                pVo.setChildren(childrenVo);
            }
            voList.add(pVo);
        }

        return voList;
    }

    @Override
    public List<UmsMenu> findByMenuIds(List<Long> menuIds) {
        return umsMenuMapper.selectBatchIds(menuIds);
    }
}
